#include "stdafx.h"
#include "Solver.h"

double* solve( int numberOfPoints, TaskType type, double gap, double x_left, double x_right, double leftGU, double rightGU )
{
	Task* task = new Task(type, gap);
	double step = (x_right - x_left) / (numberOfPoints - 1);
	double step_a = (numberOfPoints - 1) / (x_right - x_left);
	double* result = new double[numberOfPoints];
	
	double* x = new double[numberOfPoints];
	for (int i=0; i<numberOfPoints; i++)
		x[i] = x_left + i*step; 

	result[0] = leftGU;
	result[numberOfPoints-1] = rightGU;

	double *alpha = new double[numberOfPoints-1];
	double *beta = new double[numberOfPoints-1];
	double A, B, C, F, z;
	alpha[0] = 0.;
	beta[0] = leftGU;
	
	for (int i = 1; i < numberOfPoints-1; ++i)
	{	
		A = task->getAB(x[i-1], x[i]);
		B = task->getAB(x[i], x[i+1]);
		C = A + B + task->getC(x[i] - step*0.5, x[i] + step*0.5);
		F = task->getPhi(x[i] - step*0.5, x[i] + step*0.5);
		z = C - A * alpha[i - 1];
		alpha[i] = B / z;
		beta[i] = (F + A * beta[i - 1]) / z;
	}

	for (int i = numberOfPoints - 2; i > 0; --i)
		result[i] = alpha[i] * result[i+1] + beta[i];

	delete task;
	delete[] x;
	delete[] alpha;
	delete[] beta;
	return result;
}